package com.example.wangyuyuan.androidframe.mvp.base;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.wangyuyuan.androidframe.R;
import com.example.wangyuyuan.androidframe.dialog.MyDialogHint;

import butterknife.ButterKnife;

/**
 * Created by wangyuyuan on 2017/5/9.
 */

public abstract class BaseFragment extends Fragment {

    private View mRootView;
    private Dialog loadingDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = initView(inflater, container);
        ButterKnife.bind(this, mRootView);//绑定到butterknife
        return mRootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initListener();
        initData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    public void showLoading() {
        hideLoading();
        Context mContext = getContext();
        if (mContext == null) return;
        loadingDialog = new AlertDialog.Builder(mContext).create();
        loadingDialog.setCanceledOnTouchOutside(true);
        loadingDialog.setCancelable(false);
        loadingDialog.show();
        Window window = loadingDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = 1.0f;//alpha在0.0f到1.0f之间。1.0完全不透明，0.0f完全透明
        lp.dimAmount = 0.4f;//dimAmount在0.0f和1.0f之间，0.0f完全不暗，1.0f全暗
        window.setAttributes(lp);
        window.setBackgroundDrawable(new BitmapDrawable());
        View alertView = LayoutInflater.from(mContext).inflate(R.layout.layout_loading, null);
        ImageView iv = (ImageView) alertView.findViewById(R.id.ic_loading);
        Animation mAnimation = AnimationUtils.loadAnimation(mContext, R.anim.anim_loading);
        iv.setAnimation(mAnimation);
        mAnimation.start();
        window.setContentView(alertView);
    }

    /**
     * 点击屏幕或者按返回键loading
     *
     * @param ontTouchDismiss true消失,false不消失
     */
    public void showLoading(boolean ontTouchDismiss) {
        hideLoading();
        Context mContext = getContext();
        if (mContext == null) return;
        loadingDialog = new AlertDialog.Builder(mContext).create();
        loadingDialog.setCanceledOnTouchOutside(ontTouchDismiss);
        loadingDialog.setCancelable(ontTouchDismiss);
        loadingDialog.show();
        Window window = loadingDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = 1.0f;//alpha在0.0f到1.0f之间。1.0完全不透明，0.0f完全透明
        lp.dimAmount = 0.4f;//dimAmount在0.0f和1.0f之间，0.0f完全不暗，1.0f全暗
        window.setAttributes(lp);
        window.setBackgroundDrawable(new BitmapDrawable());
        View alertView = LayoutInflater.from(mContext).inflate(R.layout.layout_loading, null);
        ImageView iv = (ImageView) alertView.findViewById(R.id.ic_loading);
        Animation mAnimation = AnimationUtils.loadAnimation(mContext, R.anim.anim_loading);
        iv.setAnimation(mAnimation);
        mAnimation.start();
        window.setContentView(alertView);
    }

    public void hideLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }


    protected abstract View initView(LayoutInflater inflater, ViewGroup container);

    protected abstract void initListener();

    protected abstract void initData();


    public void setEditAndCommit(EditText editAndCommit) {
        editAndCommit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
        editAndCommit.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(".") && dest.toString().length() == 0) {
                    return "0.";
                }
                if (dest.toString().contains(".")) {
                    int index = dest.toString().indexOf(".");
                    int mlength = dest.toString().substring(index).length();
                    if (mlength == 3) {
                        return "";
                    }
                }
                return null;
            }
        }});
    }
}
